// pages/readWorks/receiveobject/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    njmaps: [],
    selectmaps: [],
    wxheight: wx.getSystemInfoSync().windowHeight,
    conheight: 500
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 查询年级和班级
    this.listNjBj();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      conheight: this.data.wxheight - 80
    })
  },
 
  checkAllChange(e) {
      const nj = e.target.dataset.nj
      const chec = e.target.dataset.chec
      var njmaps = this.data.njmaps
      for (var k = 0; k<njmaps.length; k++) {
        if (njmaps[k].nj == nj) {
            njmaps[k].chec = !chec
            var bjmaps = njmaps[k].bjlist
            for (var m = 0; m < bjmaps.length; m++) {
              bjmaps[m].chec = !chec
            }
        }
      }
      this.setData({
        njmaps: njmaps
      })
  },

  checkItemChange(e) {
      const nj = e.target.dataset.nj
      const bj = e.target.dataset.bj
      const chec = e.target.dataset.chec

      var njmaps = this.data.njmaps
      for (var k = 0; k<njmaps.length; k++) {
        if (njmaps[k].nj == nj) {
            var bjmaps = njmaps[k].bjlist
            for (var m = 0; m < bjmaps.length; m++) {
              if (bjmaps[m].bj == bj) {
                bjmaps[m].chec = !chec
              }
            }
        }
      }
      this.setData({
        njmaps: njmaps
      })
  },

  listNjBj () {
    var that = this;
    app.wxget({
      url: 'issuedWorks/listNjBj.html',
      success: function (res) {
        var njmaps = res.data;
        that.setData({
          njmaps: njmaps
        })
      }
    })
  },

  sureObject () {
      var njmaps = this.data.njmaps
      var selectmaps = []
      var selectstr= ''
      for (var k = 0; k<njmaps.length; k++) {
        if (njmaps[k].chec) {
          var secmap = {}
          secmap.nj = njmaps[k].nj
          if (selectstr) {
            selectstr+=","+njmaps[k].njname
          }else{
            selectstr = njmaps[k].njname
          }
          secmap.bj = ''
          secmap.usid = ''
          selectmaps.push(secmap)
        }else{
          var bjmaps = njmaps[k].bjlist
          for (var m = 0; m < bjmaps.length; m++) {
            if (bjmaps[m].chec) {
              var secmap = {}
              secmap.nj = njmaps[k].nj
              secmap.bj = bjmaps[m].bj
              secmap.usid = ''
              selectmaps.push(secmap)
              if (selectstr) {
                selectstr+=","+bjmaps[m].bjname
              }else{
                selectstr = bjmaps[m].bjname
              }
            }
          }
        }
      }
      this.setData({
        selectmaps: selectmaps
      })
      let pages = getCurrentPages()
      let prePage = pages[pages.length - 2]
      prePage.setData({
        selectObject: selectmaps,
        selectstr: selectstr
      })
      wx.navigateBack({
        delta: 1
      })
  }
})